package com.custom.util

import android.content.Context
import android.graphics.Color
import android.graphics.Point
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView

/*** 扩展视图可见性 ***/
fun View.setBackgroundColor(strColor: String?) {
    if (strColor != null) {
        if (strColor.length == 6 || strColor.length == 7) {
            var color_: String = strColor
            if (color_.contains("#")) {
            } else {
                color_ = "#$color_"
            }
            this.setBackgroundColor(Color.parseColor(color_))
        }
    }
}

fun getView(layout: Int): View {
    return (MiApplication.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?)?.inflate(
        layout,
        null
    )!!
}

/*** 扩展视图可见性 ***/
fun ImageView.setSrc(image: Int) {
    this.setImageDrawable(resources.getDrawable(image))
}

/*** 扩展视图可见性 ***/
fun TextView.setTextColor(color: String?) {
    if (color != null) {
        if (color.length == 6 || color.length == 7) {
            var color_: String = color
            if (color_.contains("#")) {
            } else {
                color_ = "#$color_"
            }
            //                LogKit.E("setTextColor ---->   ", "  " + color_);
            this.setTextColor(Color.parseColor(color_))
        }

    }
}

/*** 扩展视图可见性 ***/
fun setTextColor(v: TextView, strColor: Int) {
    v.setTextColor(MiApplication.context.resources.getColor(strColor))
}

/*** 扩展视图可见性 ***/
fun View.setVisible(visible: Boolean) {
    this.visibility = if (visible) View.VISIBLE else View.GONE
}


/*** 操作 宽高  放的单位是 px ***/
fun View.setWidth(px: Int) {
    this.layoutParams.width = px
}

fun View.setWidthWrap(px: Int) {
    this.layoutParams.width = px
    this.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
}

fun View.setWidthMatch(px: Int) {
    this.layoutParams.width = px
    this.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}
//        mLeftImage.(isShowLeftImage)

fun View.setHeight(px: Int) {
    this.layoutParams.height = px
}

fun View.setHeightWrap(px: Int) {
    this.layoutParams.height = px
    this.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
}

fun View.setHeightMatch(px: Int) {
    this.layoutParams.height = px
    this.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
}

fun View.addWidth(px: Int) {
    this.layoutParams.width += px
}

fun View.addHeight(px: Int) {
    this.layoutParams.height += px
}

fun View.addWidthHeight(px1: Int, px2: Int) {
    this.layoutParams.width += px1
    this.layoutParams.height += px2
}

fun View.delWidth(px: Int) {
    this.layoutParams.width -= px
}

fun View.delHeight(px: Int) {
    this.layoutParams.height -= px
}

fun View.delWidthHeight(px1: Int, px2: Int) {
    this.layoutParams.width -= px1
    this.layoutParams.height -= px2
}


fun View.setWrapWrap(): View {
    this.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
    this.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
    return this
}

fun View.setMatchMatch(): View {
    this.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
    this.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
    return this
}

fun View.setWrapMatch(): View {
    this.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
    this.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
    return this
}

fun View.setMatchWrap(): View {
    this.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
    this.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
    return this
}

/*** 操作 margin  放的 单位是 px ***/
fun View.setMargin(px: Int) {
    (layoutParams as ViewGroup.MarginLayoutParams).topMargin = px
    (layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = px
    (layoutParams as ViewGroup.MarginLayoutParams).marginStart = px
    (layoutParams as ViewGroup.MarginLayoutParams).marginEnd = px
}

fun View.setMarginTop(px: Int) {
    (layoutParams as ViewGroup.MarginLayoutParams).topMargin = px
}

fun View.setMarginBottom(px: Int) {
    (layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = px
}

fun View.setMarginStart(px: Int) {
    (layoutParams as ViewGroup.MarginLayoutParams).marginStart = px
}

fun View.setMarginEnd(px: Int) {
    (layoutParams as ViewGroup.MarginLayoutParams).marginEnd = px
}

/***增加 View Margin */
fun View.addMargin(marLeft: Int, marTop: Int, marRight: Int, marBottom: Int) {
    val lp = this.layoutParams
    if (lp is ViewGroup.MarginLayoutParams) {
        lp.leftMargin += marLeft//增高
        lp.topMargin += marTop//增高
        lp.rightMargin += marRight//增高
        lp.bottomMargin += marBottom//增高
    }
    this.layoutParams = lp
}

fun dp2px(dp: Int): Int {
    return (dp * MiApplication.context.resources.displayMetrics.density + 0.5f).toInt()
}

fun px2dp(px: Int): Int {
    return (px / MiApplication.context.resources.displayMetrics.density + 0.5f).toInt()
}

//返回屏幕高度（以像素为单位）
fun getScreenWidth(): Int {
    val wm = MiApplication.context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val size = Point()
    wm.defaultDisplay.getSize(size)
    return size.x
}


